package dnuifood_cjy.dnuifood_16110100529.controller;


import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import java.util.List;

import dnuifood_cjy.dnuifood_16110100529.Adapter.SearchListAdapter;
import dnuifood_cjy.dnuifood_16110100529.R;
import dnuifood_cjy.dnuifood_16110100529.bean.SearchBean;
import dnuifood_cjy.dnuifood_16110100529.listener.OnRetrofitListener;
import dnuifood_cjy.dnuifood_16110100529.model.FoodModel;

public class SearchResultActivity extends BaseActivity implements OnRetrofitListener<List<SearchBean>> {

    private RecyclerView recyclerView;
    private String search;
    private ImageView back;

    @Override
    void initViews() {

        setLayout(R.layout.activity_search_result);
        back=(ImageView)findViewById(R.id.imageView11);
        recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    void initEvents() {

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(SearchFragment.this);
//                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    void initData() {
        Intent intent = getIntent();
        search=intent.getStringExtra("search");
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodBySearch(search,this);
    }


    @Override
    public void onSuccess(List<SearchBean> list, int flag) {
        if (list!=null&&list.size() > 0) {
            SearchListAdapter adapter = new SearchListAdapter(SearchResultActivity.this, list, R.layout.shop_item2,null);
            recyclerView.setAdapter(adapter); } else {
            showToast("没有搜索结果");
            finish(); }
    }

    @Override
    public void onFailure(String msg) {
        showToast("网络发生错误");
    }
}

